package com.zrt.pad.datasource;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zrt.pad.constfields.ConstFielsdes;
import com.zrt.pad.datasource.db.CustomDBHelper;
import com.zrt.pad.datasource.bean.*;
import com.zrt.pad.util.NumberUtil;
import com.zrt.pad.util.SellSaveInterface;
import com.zrt.pad.util.SellUtil;
import com.zrt.pad.util.U;

public class DataSourceInstance {

	public static final DataSourceInstance instance = new DataSourceInstance();

	public DataSourceInstance() {

	}

	public long setOption(String sysoption, String value) {
		long row;
		SQLiteDatabase db = CustomDBHelper.instance.getWritableDatabase();
		Cursor c = db.rawQuery("SELECT * FROM tSysOption where SysOption=?",
				new String[] { sysoption });
		if (c.getCount() > 0) {
			db.beginTransaction();
			ContentValues v = new ContentValues();
			v.put("OPTIONVALUE", value);
			row = db.update("tSysOption", v, "SysOption=?", new String[] { sysoption });
			db.setTransactionSuccessful();
			db.endTransaction();
		} else {
			row = -2;
		}
		c.close();
		return row;
	}

	public String getOption(String sysoption) {
		String value = null;
		SQLiteDatabase db = CustomDBHelper.instance.getReadableDatabase();
		Cursor c = db.rawQuery("SELECT OptionValue FROM tSysOption WHERE SysOption = ?",
				new String[] { sysoption });
		if (c.moveToNext()) {
			value = c.getString(0);
		}
		c.close();
		return value;
	}
	
	public void saveSysOption(SysOption[] opts) {
		SQLiteDatabase db = CustomDBHelper.instance.getWritableDatabase();
		db.beginTransaction();
		db.delete("tSysOption", "STATIONID <> ?", new String[] {"*"});
		for (SysOption m : opts) {
	    	ContentValues v = new ContentValues();
		    v.put("STATIONID", m.getStationid());
			v.put("ORGCODE", m.getOrgcode());
			v.put("SYSOPTION", m.getSysoption());
			v.put("SYSOPTIONNAME", m.getSysoptionname());
			v.put("OPTIONVALUE", m.getOptionvalue());
			v.put("REMARK", m.getRemark());
			db.insertWithOnConflict("tSysOption", null, v, SQLiteDatabase.CONFLICT_REPLACE);
		}
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	
	
	public void saveUsrUser(UsrUser[] users) {
		SQLiteDatabase db = CustomDBHelper.instance.getWritableDatabase();
		db.beginTransaction();
		db.delete("tUsrUser", "STATIONID <> ?", new String[] {"*"});
		for (UsrUser m : users) {
			ContentValues v = new ContentValues();
			v.put("STATIONID", m.getStationid());
			v.put("ORGCODE", m.getOrgcode());
			v.put("DEPCODE", m.getDepcode());
			v.put("USERCODE", m.getUsercode());
			v.put("USERNAME", m.getUsername());
			v.put("USERTYPE", m.getUsertype());
			v.put("PASSWD", m.getPasswd());
			v.put("USERIMAGE", m.getUserimage());
			v.put("USERRULE", m.getUserrule());
			v.put("ISACTIVE", m.getIsactive());
			v.put("REMARK", m.getRemark());
			db.insertWithOnConflict("tUsrUser", null, v, SQLiteDatabase.CONFLICT_REPLACE);
		}
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	
	//查询本地数据
	public List<SysOption> querySysOption() {
		SQLiteDatabase db = CustomDBHelper.instance.getReadableDatabase();
		List<SysOption> list = new ArrayList<SysOption>();
		Cursor c = db.rawQuery(
				"select * from tSysOption", null);
		while (c.moveToNext()) {
			SysOption item = new SysOption();
			item.setStationid(c.getString(c.getColumnIndex("StationID"))); 
			item.setOrgcode(c.getString(c.getColumnIndex("OrgCode")));
			item.setSysoption(c.getString(c.getColumnIndex("SysOption")));
			item.setSysoptionname(c.getString(c.getColumnIndex("SysOptionName")));
			item.setOptionvalue(c.getString(c.getColumnIndex("OptionValue")));
			item.setRemark(c.getString(c.getColumnIndex("Remark")));
			list.add(item);
		}
		c.close();
		return list;
	}

	public List<UsrUser> queryUsrUser() {
		SQLiteDatabase db = CustomDBHelper.instance.getReadableDatabase();
		List<UsrUser> list = new ArrayList<UsrUser>();
		Cursor c = db.rawQuery(
				"select * from tUsrUser where IsActive =?", new String[] {"1"});
		while (c.moveToNext()) {
			UsrUser item = new UsrUser();
			item.setStationid(c.getString(c.getColumnIndex("StationID"))); 
			item.setOrgcode(c.getString(c.getColumnIndex("OrgCode")));
			item.setDepcode(c.getString(c.getColumnIndex("DepCode")));
			item.setUsercode(c.getString(c.getColumnIndex("UserCode")));
			item.setUsername(c.getString(c.getColumnIndex("UserName")));
			item.setUsertype(c.getString(c.getColumnIndex("UserType")));
			item.setPasswd(c.getString(c.getColumnIndex("Passwd")));
			item.setUserimage(c.getString(c.getColumnIndex("UserImage")));
			item.setUserrule(c.getString(c.getColumnIndex("UserRule")));
			item.setIsactive(c.getString(c.getColumnIndex("IsActive")));
			item.setRemark(c.getString(c.getColumnIndex("Remark")));
			list.add(item);
		}
		c.close();
		return list;
	}
	
}
